//
//  DetailView.m
//  UI-21-豆豆豆瓣
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 Wang Hao. All rights reserved.
//

#import "DetailView.h"
#import "UIImageView+WebCache.h"
#import "DatabaseSingleton.h"

@implementation DetailView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)dealloc
{
    [_headImageView release];
    [_nameLabel release];
    [_plot_simpleLabel release];
    [_genresLabel release];
    [_countryLabel release];
    [_runtimeLabel release];
    [_rating_countLabel release];
    [_ratingLabel release];
    [_actorsLabel release];
    [_release_dateLabel release];
    [_collectButton release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self createSubs];
    }
    return self;
    
    
}

-(void)createSubs{
    
    self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 180)];
    [self addSubview:self.headImageView];
    [_headImageView release];
    //评分
    self.ratingLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 25, 100, 30)];
    [self addSubview:self.ratingLabel];
    [_ratingLabel release];
    //评论人数
    self.rating_countLabel = [[UILabel alloc] initWithFrame:CGRectMake(240, 25, 100, 30)];
    [self addSubview:self.rating_countLabel];
    [_rating_countLabel release];
    //上映时间
    self.release_dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 60, 100, 30)];
    [self addSubview:self.release_dateLabel];
    [self.release_dateLabel release];
    //时间
    self.runtimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 95, 100, 30)];
    [self addSubview:self.runtimeLabel];
    [_runtimeLabel release];
    //分类
    self.genresLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 130, 200, 30)];
    [self addSubview:self.genresLabel];
    [self.genresLabel release];
    //国家
    self.countryLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 165, 200, 30)];
    [self addSubview:self.countryLabel];
    [_countryLabel release];
    //制作人
    UILabel *actors_Head_Label = [[UILabel alloc] initWithFrame:CGRectMake(20, 230, 100, 50)];
    actors_Head_Label.text = @"制作人";
    actors_Head_Label.font = [UIFont systemFontOfSize:20];
    [self addSubview:actors_Head_Label];
    [actors_Head_Label release];
    //制作人信息
    self.actorsLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 260, 325, 100)];
    self.actorsLabel.font = [UIFont systemFontOfSize:15];
    self.actorsLabel.numberOfLines = 0;
    [self addSubview:self.actorsLabel];
    [self.actorsLabel release];
    //电影情节
    self.plot_simpleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 340, 325, 50)];
    self.plot_simpleLabel.text = @"电影情节";
    self.plot_simpleLabel.font = [UIFont systemFontOfSize:20];
    [self addSubview:self.plot_simpleLabel];
    [self.plot_simpleLabel release];
    //电影简介
    self.plot_simpleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 410, 325, 303)];
    self.plot_simpleLabel.numberOfLines = 0;
    self.plot_simpleLabel.font = [UIFont systemFontOfSize:17];
    [self addSubview:self.plot_simpleLabel];
    [self.plot_simpleLabel release];
    //收藏
    self.collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.collectButton.frame = CGRectMake(220, 245, 20, 20);
    [self.collectButton setBackgroundImage:[UIImage imageNamed:@"ic_star_half@2x"] forState:UIControlStateNormal];
    
    
    self.collectButton.adjustsImageWhenHighlighted = NO;
    [self addSubview:self.collectButton];
    [self.collectButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}
#warning 收藏
-(void)buttonAction:(UIButton *)but{
    
    [self.collectDelegate collect];
//    NSString *nameU = [[NSUserDefaults standardUserDefaults] objectForKey:@"用户名"];
//    //NSLog(@"+++++++++++++++++++++nameU++++++%@",nameU);
//    
//    NSString *a = [[NSUserDefaults standardUserDefaults] objectForKey:@"1"];
//    if ([a isEqualToString:@"1"]) {
//        
//        [[DatabaseSingleton shareDatabaseSingleton] openDB];
//        NSMutableArray *array = [[DatabaseSingleton shareDatabaseSingleton] selectcollect:nameU];
//        NSMutableArray *movieNameArray = [NSMutableArray array];
//        for (Movie *mov in array) {
//            
//            [movieNameArray addObject:mov.release_date];
//        }
//        BOOL result = [movieNameArray containsObject:self.movDetail.release_date];
//        //NSLog(@"==============self.movie.release_date%@",self.movDetail.release_date);
//        if (result) {
//            
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经收藏过" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alert show];
//
//            
//            
//        }else{
//            
//            [self.collectButton setBackgroundImage:[UIImage imageNamed:@"ic_star_red@2x"] forState:UIControlStateNormal];
//            [[DatabaseSingleton shareDatabaseSingleton] insertData:self.movDetail name:nameU];
//            NSLog(@"====%@", self.movDetail.movieName);
//            [[DatabaseSingleton shareDatabaseSingleton] insertcollect:self.movDetail name:nameU];
//            //NSLog(@"==================%@",self.myDetail.movDetail);
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alert show];
//        }
//    }else{
//        
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alert show];
//        
//    }

    
}

-(void)setMovDetail:(Movie *)movDetail{
    if (_movDetail != movDetail) {
        
        [_movDetail release];
        _movDetail = [movDetail retain];
    }
    
    self.movDetail.movieName =
    self.countryLabel.text = self.movDetail.film_locations;
    self.release_dateLabel.text = self.movDetail.release_date;
    self.actorsLabel.text = self.movDetail.actors;
    self.genresLabel.text = self.movDetail.genres;
    self.runtimeLabel.text = self.movDetail.runtime;
    self.plot_simpleLabel.text = self.movDetail.plot_simple;
    //[self.plot_simpleLabel sizeToFit];
    
    NSString *pop = [NSString stringWithFormat:@"评分: %@",self.movDetail.rating];
    self.ratingLabel.text = pop;
    
    NSString *pp = [NSString stringWithFormat:@"(%@)评论",self.movDetail.rating_count];
    self.rating_countLabel.text = pp;
    
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:self.movDetail.poster] placeholderImage:[UIImage imageNamed:@"AppIcon60x60@2x"]];
    //计算高度
    CGFloat height = [[self class] heightForPlot:self.movDetail.plot_simple];
    CGRect tempFrame = self.plot_simpleLabel.frame;
    tempFrame.size.height = height;
    self.plot_simpleLabel.frame = tempFrame;
    
    self.height = height;
#warning 收藏
    NSString *a = [[NSUserDefaults standardUserDefaults] objectForKey:@"1"];

    if ([a isEqualToString:@"1"]){
    NSString *nameU = [[NSUserDefaults standardUserDefaults] objectForKey:@"用户名"];
    NSMutableArray *array = [[DatabaseSingleton shareDatabaseSingleton] selectcollect:nameU];
    for (Movie *mov in array) {
        
        if ([mov.release_date isEqualToString:self.movDetail.release_date]) {
            
            [self.collectButton setBackgroundImage:[UIImage imageNamed:@"ic_star_red@2x"] forState:UIControlStateNormal];
        }
        
    }
    }
    
}
//高度自适应
+(CGFloat)heightForPlot:(NSString *)plot{
    
    CGSize size = CGSizeMake(325, 100000);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
    CGRect frame = [plot boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;
    
}

@end
